#TODO:   1. s is type str, s.strip() is not empty, return s.strip()


class Str(object):
    @staticmethod
    def from_bytes(src, encoding="utf-8"):
        if src is not None and type(src) is bytes:
            return src.decode(encoding)
        return Str("")

    def __init__(self, s):
        self.__str = s

    def is_empty(self):
        return self.__str is None or self.__str == ""
    def is_not_empty(self):
        return self.__str is not None and self.__str != ""

    def is_empty_or_space(self):
        return self.is_empty() or self.__str.isspace()

    def to_bytes(self, encoding="utf-8"):
        if self.is_not_empty():
            return self.__str.encode(encoding)
        return bytes()
    

    def str(self):
        return self.__str

import unittest
class TestStr(unittest.TestCase):
    def test_string_wrapper(self):
        self.assertTrue(Str(None).is_empty())
        self.assertTrue(Str("").is_empty())
        self.assertFalse(Str("hello").is_empty())

        self.assertFalse(Str(None).is_not_empty())
        self.assertFalse(Str("").is_not_empty())
        self.assertTrue(Str("hello").is_not_empty())

        self.assertTrue(Str(None).is_empty_or_space())
        self.assertTrue(Str("").is_empty_or_space())
        self.assertTrue(Str("   ").is_empty_or_space())
        self.assertFalse(Str("hello").is_empty_or_space())

        s = "abcde"
        bytes_s  = s.encode('utf-8')

        self.assertEqual(bytes_s, Str(s).to_bytes())
        self.assertEqual(s, Str.from_bytes(bytes_s))

    def test_is_empty(self):
        s = Str("")
        self.assertTrue(s.is_empty())

        s = Str(None)
        self.assertTrue(s.is_empty())

        s = Str("Hello")
        self.assertFalse(s.is_empty())

    def test_is_not_empty(self):
            # Test when the string is not empty
            s = Str("Hello")
            self.assertTrue(s.is_not_empty())

            # Test when the string is empty
            s = Str("")
            self.assertFalse(s.is_not_empty())

            # Test when the string is None
            s = Str(None)
            self.assertFalse(s.is_not_empty())

    def test_is_empty_or_space_non_empty_non_space_string(self):
        # Test when the string is not empty and does not contain only spaces
        s = Str("Hello")
        self.assertFalse(s.is_empty_or_space()) 

        # Test when the string contains only spaces
        s = Str("   ")
        self.assertTrue(s.is_empty_or_space())

        # Test when the string is not empty and does not contain only spaces
        s = Str("Hello")
        self.assertFalse(s.is_empty_or_space())


    def test_to_bytes_empty_string(self):
        s = Str("")
        self.assertEqual(s.to_bytes(), bytes())

    def test_to_bytes_non_empty_string(self):
        s = Str("Hello")
        self.assertEqual(s.to_bytes(), b"Hello")

    def test_to_bytes_different_encoding(self):
        s = Str("你好")
        self.assertEqual(s.to_bytes("gbk"), b"\xc4\xe3\xba\xc3")
        self.assertEqual(s.to_bytes("utf-8"), b'\xe4\xbd\xa0\xe5\xa5\xbd')
        self.assertEqual(s.to_bytes(), b'\xe4\xbd\xa0\xe5\xa5\xbd')
        

if __name__ == '__main__':
    unittest.main(verbosity=2)
